<?php

namespace App\model;

use DateTimeInterface;

class User extends Base
{

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $table = 'em';
    public $casts = [
        'created_at' => 'datetime:Y-m-d H:i:s',
        'updated_at' => 'datetime:Y-m-d H:i:s',
        'edit_at' => 'datetime:Y-m-d H:i:s'
    ];


    /**
     * ----------------------------------------------
     * 关联模型
     * ---------------------------------------------
     */
    public function posts(){
        return $this->hasMany(Post::class, 'user_id');
    }
    public function comments(){
        return $this->hasMany(Comment::class, 'user_id');
    }
    //关注的学校
    public function followSchools()
    {
        return $this->belongsToMany(School::class, UserFollowSchool::class, 'user_id', 'school_id')
            ->withPivot('created_at as sort')
            ->orderByDesc('sort');
    }
    public function viewSchools()
    {
        return $this->belongsToMany(School::class, UserViewSchoolLog::class, 'user_id', 'school_id')
            ->withPivot('created_at as sort')
            ->orderByDesc('sort')->groupBy('schools.id');
    }
    public function followTopics()
    {
        return $this->belongsToMany(Topic::class, UserFollowTopic::class, 'user_id', 'topic_id')
            ->select(['topics.id', 'topics.title'])
            ->withPivot('created_at as sort')
            ->withCount('posts', 'comments', 'views')
            ->where('topics.status', 1)
            ->orderByDesc('sort');
    }
    public function viewTopics()
    {
        return $this->belongsToMany(Topic::class, UserViewTopicLog::class, 'user_id', 'topic_id')
            ->select(['topics.id', 'topics.title'])
            ->withPivot('created_at as sort')
            ->withCount('posts', 'comments', 'views')
            ->where('topics.status', 1)
            ->distinct('topic.id')->orderByDesc('sort')->groupBy('topics.id');
    }


    /**
     * --------------------------------------------
     * 后台方法
     * -------------------------------------------
     * @param $filter
     * @return array
     */
    public function getList($filter)
    {
        return $this->select(['id', 'avatarUrl', 'username', 'nickName', 'phone', 'age', 'created_at', 'login_at'])->withCount('plays')
            ->when(!empty($filter['name']), function ($query) use($filter){
                return $query->where('username', 'like', "%{$filter['name']}%");
            })->when(!empty($filter['nickName']), function ($query) use($filter){
                return $query->where('nickName', 'like', "%{$filter['nickName']}%");
            })->when(!empty($filter['phone']), function ($query) use($filter){
                return $query->where('phone', 'like', "%{$filter['phone']}%");
            })->when(!empty($filter['create_time']) and is_array($filter['create_time']) and count($filter['create_time']) == 2 and !empty($filter['create_time'][0]),
                function ($query) use($filter){
                    return $query->whereBetween('created_at', [$filter['create_time'][0].' 00:00:00', $filter['create_time'][1].' 23:59:59']);
                }
            )->when(!empty($filter['login_time']) and is_array($filter['login_time']) and count($filter['login_time']) == 2 and !empty($filter['login_time'][0]),
                function ($query) use($filter){
                    return $query->whereBetween('login_at', [$filter['login_time'][0].' 00:00:00', $filter['login_time'][1].' 23:59:59']);
                }
            )->orderBy('id', 'desc')->when(!empty($filter['isExport']) and $filter['isExport'], function($query) use($filter){
                return $query->get();
            },function($query) use($filter) {
                return $query->paginate($filter['pageSize'] ?? 10);
            });
    }

}
